package com.maiji.cloud.controller;

import com.maiji.cloud.entities.manager.Menu;
import com.maiji.cloud.entities.manager.Role;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.manager.*;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.manager.UserResData;
import com.maiji.cloud.service.RoleMenuService;
import com.maiji.cloud.service.RoleService;
import com.maiji.cloud.service.UserRoleService;
import com.maiji.cloud.service.UserService;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/manager/")
public class ManagerController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleMenuService roleMenuService;

    /**
     *  获取角色列表
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("findAllRoles")
    public BaseMetaResDto<List<Role>> findAllRoles (@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String roleName = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        return roleService.findAllRoles(roleName, metaData);
    }

    /**
     *  修改角色数据
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("updRole")
    public BaseResDto updRole (@RequestBody BaseDataReqDto<Role> baseDataReqDto) {
        Role role = baseDataReqDto.getData();
        return roleService.updRole(role);
    }

    /**
     *  添加角色
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("addRole")
    public BaseResDto addRole (@RequestBody BaseDataReqDto<RoleAddReqData> baseDataReqDto) {
        RoleAddReqData data = baseDataReqDto.getData();
        return roleService.addRole(data);
    }

    /**
     *  删除角色
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("delRole")
    public BaseResDto delRole (@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String roleId = baseDataReqDto.getData();
        if (StringUtil.isBlank(roleId)) return new BaseResDto(Status.PARAMETERERROR);
        return roleService.delRole(roleId);
    }

    /**
     *  添加用户
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("addUser")
    public BaseResDto addUser (@RequestBody BaseDataReqDto<UserAddReqData> baseDataReqDto) {
        UserAddReqData data = baseDataReqDto.getData();
        return userService.addUser(data);
    }

    /**
     *  获取用户列表
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("findAllUsers")
    public BaseMetaResDto<List<UserResData>> findAllUsers (@RequestBody BaseDataReqDto<UserFindAllReqData> baseDataReqDto) {
        String userName = baseDataReqDto.getData().getName();
        String roleId = baseDataReqDto.getData().getRoleId();
        Boolean eqRoleId = baseDataReqDto.getData().getEqRoleId();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        return userService.findAllUsers(roleId, userName, eqRoleId, metaData);
    }

    /**
     *  修改用户数据
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("updUser")
    public BaseResDto updUser (@RequestBody BaseDataReqDto<UserUpdReqData> baseDataReqDto) {
        UserUpdReqData data = baseDataReqDto.getData();
        return userService.updUser(data);
    }

    /**
     *  编辑用户角色
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("editUserRole")
    public BaseResDto editUserRole (@RequestBody BaseDataReqDto<UserRoleEditReqData> baseDataReqDto) {
        UserRoleEditReqData data = baseDataReqDto.getData();
        return userRoleService.editUserRole(data);
    }

    /**
     *  编辑角色用户
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("editRoleUser")
    public BaseResDto editRoleUser (@RequestBody BaseDataReqDto<RoleUserEditReqData> baseDataReqDto) {
        String roleId = baseDataReqDto.getData().getRoleId();
        List<String> userIds = baseDataReqDto.getData().getUserIds();
        Boolean add = baseDataReqDto.getData().getAdd();
        if (StringUtil.isBlank(roleId) || userIds == null || userIds.size() == 0 || add == null)
            return new BaseResDto(Status.PARAMETERERROR);
        return userRoleService.editRoleUser(roleId, userIds, add);
    }

    /**
     *  获取角色的权限菜单
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("findAllMenus")
    public BaseDataResDto<List<Menu>> findAllMenus (@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String roleId = baseDataReqDto.getData();
        return roleMenuService.findAllMenus(roleId);
    }

    /**
     *  编辑角色的权限菜单
     * @param baseDataReqDto
     * @return
     */
    @RequestMapping("editRoleMenu")
    public BaseResDto editRoleMenu (@RequestBody BaseDataReqDto<RoleMenuEditReqData> baseDataReqDto) {
        String roleId = baseDataReqDto.getData().getRoleId();
        List<String> menuIds = baseDataReqDto.getData().getMenuIds();
        Boolean add = baseDataReqDto.getData().getAdd();
        if (StringUtil.isBlank(roleId) || menuIds == null || menuIds.size() == 0 || add == null)
            return new BaseResDto(Status.PARAMETERERROR);
        return roleMenuService.editRoleMenu(roleId, menuIds, add);
    }

}
